package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	public class Head extends Sprite
	{
		/**
		 * 速度 
		 */		
		private var vx:Number = 12;
		private var vy:Number = 0;
		/**
		 * 上次坐标 
		 */		
		public var lastPoint:Point;
		
		public function Head()
		{
			super();
			
			draw();
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
			
		}
		
		protected function onAddedToStage(event:Event):void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
		}
		/**
		 * 键盘事件处理 
		 * @param event
		 * 
		 */		
		protected function onStageKeyDown(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.LEFT:
					vx = -12;
					vy = 0;
					break;
				case Keyboard.RIGHT:
					vx = 12;
					vy = 0;
					break;
				case Keyboard.UP:
					vx = 0;
					vy = -12;
					break;
				case Keyboard.DOWN:
					vx = 0;
					vy = 12;
					break;
			}
		}
		/**
		 * 画矩形 
		 * 
		 */		
		private function draw():void
		{
			var g:Graphics = this.graphics;
			g.beginFill( 0x336699 );
			g.drawRect(0,0,10,10);
			g.endFill();
		}
		/**
		 * 更新坐标 
		 * 
		 */		
		public function update():void
		{
			// 记录上次坐标
			lastPoint = new Point(x,y);
			// 
			x += vx;
			y += vy;
		}
	}
}